package com.ice.body.hardware.controller;

import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.WareHouseService;
import com.ice.body.hardware.model.DeviceIo;
import com.ice.body.hardware.service.DeviceIoService;
import io.swagger.annotations.ApiOperation;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-12-01
 * Time: 05:28
 */
@RestController
@ApiIgnore(value = "io模块")
public class DeviceIoController extends BaseController{
    //---------------------------io模块-----------------------start
    @Resource
    private DeviceIoService deviceIoService;
    @Resource
    private WareHouseService wareHouseService;

    @ApiOperation(value = "io模块-新增")
    @PostMapping("/api/DeviceIo/insert")
    public AjaxResult<DeviceIo> insert_DeviceIo(DeviceIo model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            model.setCreateTime(new Date());
            deviceIoService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "io模块-删除")
    @PostMapping("/api/DeviceIo/deleteById")
    public AjaxResult<DeviceIo> deleteById_DeviceIo(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = deviceIoService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "io模块-更新")
    @PostMapping("/api/DeviceIo/updateById")
    public AjaxResult<DeviceIo> update_DeviceIo(DeviceIo model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            deviceIoService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "io模块-查询一个")
    @PostMapping("/api/DeviceIo/selectById")
    public AjaxResult<DeviceIo> selectById_DeviceIo(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           DeviceIo model = deviceIoService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "io模块-查询多个")
    @PostMapping("/api/DeviceIo/selectAll")
    public AjaxResult<DeviceIo> selectList_DeviceIo(DeviceIo model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<DeviceIo> list = deviceIoService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "io模块-查询分页")
    @PostMapping("/api/DeviceIo/selectListByPageHelper")
    public AjaxResult<DeviceIo> selectListByPageHelper_DeviceIo(@RequestParam(name = "pageSize") Integer pageSize,
                                                                @RequestParam(name = "pageNum") Integer pageNum,
                                                                DeviceIo model,
                                                                String keyWord){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
            if(StringUtils.isEmpty(keyWord)) {
                list = deviceIoService.selectListByModel(model);
            } else {
                list = deviceIoService.selectListByKeyWord(keyWord);
            }
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------io模块-----------------------end
}
